package com.tom;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MemberController
 */
public class MemberController extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final String LIST_MEMBER = "listMember.jsp";
        private static final String ADD_MEMBER = "member.jsp";
        private static final String EDIT_MEMBER = "member_edit.jsp";

        private MemberDAO dao;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public MemberController() {
                super();
                dao = new MemberDAO();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
        	doPost(request, response);
        	
        		
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {

        	request.setCharacterEncoding("utf-8");
        	String forward = LIST_MEMBER;
            String action = request.getParameter("action");
            if (action == null) {
                    forward = LIST_MEMBER;
            } else {
                    if (action.equalsIgnoreCase("listmember")) {
                            forward = LIST_MEMBER;
                    }
                    if (action.equalsIgnoreCase("addmember")) {
                            if (request.getParameter("id") != null) {
                                    // save
                                    String id = request.getParameter("id");
                                    String password1 = request.getParameter("password1");
                                    String password2 = request.getParameter("password2");
                                    String name_c = request.getParameter("name_c");
                                    String name_a = request.getParameter("name_a");
                                    String brith = request.getParameter("brith");
                                    String tel_area1 = request.getParameter("tel_area1");
                                    String telephone1 = request.getParameter("telephone1");
                                    String telephone1_p = request.getParameter("telephone1_p");
                                    String mobile1 = request.getParameter("mobile1");
                                    String tel_area2 = request.getParameter("tel_area2");
                                    String telephone2 = request.getParameter("telephone2");
                                    String telephone2_p = request.getParameter("telephone2_p");
                                    String mobile2 = request.getParameter("mobile2");
                                    String address1 = request.getParameter("address1");
                                    String address2 = request.getParameter("address2");
                                    String original = request.getParameter("original");
                                    String professional = request.getParameter("professional");
                                    String company_num = request.getParameter("company_num");
                                    String company_name = request.getParameter("company_name");
                                    String company_job = request.getParameter("company_job");
                                    String company_tel_area = request.getParameter("company_tel_area");
                                    String company_tel = request.getParameter("company_tel");
                                    String company_tel_p = request.getParameter("company_tel_p");
                                    String contact_name = request.getParameter("contact_name");
                                    String contact_keep = request.getParameter("contact_keep");
                                    String contact_area = request.getParameter("contact_area");
                                    String contact_tel = request.getParameter("contact_tel");
                                    String contact_tel_p = request.getParameter("contact_tel_p");
                                    String email = request.getParameter("email");
                                    Member m = new Member(id, password1, password2, name_c, name_a, brith, tel_area1, telephone1, telephone1_p, mobile1, tel_area2, telephone2, telephone2_p, mobile2, address1, address2, original, professional, company_num, company_name, company_job, company_tel_area, company_tel, company_tel_p, contact_name, contact_keep, contact_area, contact_tel, contact_tel_p, email);
                                    dao.add(m);
                                    forward = LIST_MEMBER;
                            } else {
                                    forward = ADD_MEMBER;
                            }
                    }
                    if (action.equalsIgnoreCase("edit")) {
                    		String name = request.getParameter("id");
                            Member m = dao.getMemberById(request.getParameter(name));
                            request.setAttribute("m", m);
                            forward = EDIT_MEMBER;
                    }
            }
            List<Member> members = dao.getAll();
            request.setAttribute("members", members);
            RequestDispatcher dispatcher = request
                            .getRequestDispatcher(forward);
            dispatcher.forward(request, response);
//                String action = request.getParameter("action");
//                if (action.equalsIgnoreCase("edit")) {
//                        Member m = new Member(request.getParameter("id"),
//                                        request.getParameter("password1"),
//                                        request.getParameter("password2"), request.getParameter("name_c"),
//                                        request.getParameter("name_a"), request.getParameter("brith"),
//                                        request.getParameter("tel_area1"), request.getParameter("telephone1"),
//                                        request.getParameter("telephone1_p"), request.getParameter("mobile1"),
//                                        request.getParameter("tel_area2"), request.getParameter("telephone2"),
//                                        request.getParameter("telephone2_p"), request.getParameter("mobile2"),
//                                        request.getParameter("address1"), request.getParameter("address2"),
//                                        request.getParameter("original"), request.getParameter("professional"),
//                                        request.getParameter("company_num"), request.getParameter("company_name"),
//                                        request.getParameter("company_job"), request.getParameter("company_tel_area"),
//                                        request.getParameter("company_tel"), request.getParameter("company_tel_p"),
//                                        request.getParameter("contact_name"), request.getParameter("contact_keep"),
//                                        request.getParameter("contact_area"), request.getParameter("contact_tel"),
//                                        request.getParameter("contact_tel_p"),request.getParameter("email"));
//                        dao.update(m);
//                        List<Member> members = dao.getAll();
//                        request.setAttribute("members", members);
//                        RequestDispatcher dispatcher = request
//                                        .getRequestDispatcher(LIST_MEMBER);
//                        dispatcher.forward(request, response);
//                }

        }

}